package com.yjc.hire.mq;


import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DelayConfig_Industry {
    public static final String EXCHANGE_DELAY_REFRESH = "exchange_delay_refresh";

    public static final String QUEUE_DELAY_REFRESH = "queue_delay_refresh";

    public static final String DELAY_REFRESH_INDUSTRY = "delay.refresh.industry";

    @Bean(EXCHANGE_DELAY_REFRESH)
    public Exchange smsExchange() {
        return ExchangeBuilder.topicExchange(EXCHANGE_DELAY_REFRESH).
                delayed()
                .durable(true).build();
    }

    @Bean(QUEUE_DELAY_REFRESH)
    public Queue smsQueue() {
      return QueueBuilder.durable(QUEUE_DELAY_REFRESH)
              .build();
    }

    @Bean
    public Binding smsBindingIndustry(@Qualifier(EXCHANGE_DELAY_REFRESH) Exchange exchange, @Qualifier(QUEUE_DELAY_REFRESH) Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("delay.refresh.*")
                .noargs();
    }
    public static MessagePostProcessor setDelayTimes(Integer times){
        return new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(times);
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            }
        };
    }
}
